/*
 * * * * * * * * * * * * * * *
 *	author: 马丰彦
 *	date: 2019/1/12
 *	function: 创建url循环并爬取每个页面的详情url
 * * * * * * * * * * * * * * *
 */
const readline = require('readline');
var Crawler = require('crawler'); // node-crawler
var url = require('../model/moreLinks');
var repeat = require('../model/repeat');
var mongoose = require('mongoose');
mongoose.connect('mongodb://127.0.0.1"27017/film', {
  useNewUrlParser: true
})
const db = mongoose.connection
db.once('open', function () {
  console.log('连接数据库成功\n');
  const firms = encodeURI('电影'); // 电影
  var firmUrl = `https://movie.douban.com/j/new_search_subjects?sort=U&range=0,10&tags=${firms}&` // 前缀

  // 电影分类 22
  var firmType = [];
  const gut = encodeURI('剧情'); // 剧情
  const comedy = encodeURI('喜剧');
  const action = encodeURI('动作');
  const love = encodeURI('爱情');
  const science = encodeURI('科幻');
  const animation = encodeURI('动画');
  const sciences = encodeURI('悬疑');
  const panic = encodeURI('惊悚');
  const terror = encodeURI('恐怖');
  const crime = encodeURI('犯罪');
  const sex = encodeURI('同性');
  const music = encodeURI('音乐');
  const dance = encodeURI('歌舞');
  const memoirist = encodeURI('传记');
  const history = encodeURI('历史');
  const war = encodeURI('战争');
  const west = encodeURI('西部');
  const queer = encodeURI('奇幻');
  const adventure = encodeURI('冒险');
  const disaster = encodeURI('灾难');
  const wuxia = encodeURI('武侠');
  const sexy = encodeURI('情色');
  firmType.push(gut, comedy, action, love, science, animation, sciences, panic, terror, crime, sex, music, dance, memoirist, history, war, west, queer, adventure, disaster, wuxia, sexy);

  // 电影年代 60
  var firmYear = [];
  const earlier = '1,1959'; // 更早
  for (let i = 0; i < 10; i++) {
    firmYear.push('196' + i)
    firmYear.push('197' + i)
    firmYear.push('198' + i)
    firmYear.push('199' + i)
    firmYear.push('200' + i)
    firmYear.push('201' + i)
  }
  firmYear.push(earlier);
  var guts = [];
  for (let j = 0; j < firmType.length; j++) {
    for (let k = 0; k < firmYear.length; k++) {
      for (let p = 0; p < 3500; p = p + 20) {
        if (firmType[j] == comedy && p > 1700) { // 喜剧
          break
        } else if (firmType[j] === action || firmType[j] === love && p > 1000) { // 动作和爱情
          break
        } else if (firmType[j] === science || firmType[j] === adventure && p > 500) { // 科幻 冒险
          break
        } else if (firmType[j] === animation && p > 400) { // 动画
          break
        } else if (firmType[j] === sciences && p > 700) { // 悬疑
          break
        } else if (firmType[j] === panic && p > 1300) { // 惊悚
          break
        } else if (firmType[j] === terror && p > 1500) { // 恐怖
          break
        } else if (firmType[j] === crime || firmType[j] === crime && p > 600) { // 犯罪
          break
        } else if (firmType[j] === sex && p > 300) {
          break
        } else if (firmType[j] === music || firmType[j] === dance || firmType[j] === memoirist || firmType[j] === disaster || firmType[j] === wuxia || firmType[j] === sexy && p > 200) {
          break
        }
        var obj = {
          url: `${firmUrl}start=${p}&genres=${firmType[j]}&year_range=${firmYear[k]},${firmYear[k]}`
        }
        guts.push(obj);
      }
    }
  }

  // 打乱数组
  function shuffle(arr) {
    let i = arr.length;
    while (i) {
      let j = Math.floor(Math.random() * i--);
      [arr[j], arr[i]] = [arr[i], arr[j]];
    }
    return arr;
  }

  const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
    prompt: '请输入代理ip(直接按enter为不填)> '
  });
  rl.prompt();
  rl.on('line', (line) => {
    var http = null;
    if (!!line) {
      http = 'http://' + line;
    }
    var nums = 0;
    var linksss = '';
    var check = new Crawler({
      maxConnections: 10,
      jQuery: false,
      proxy: http,
      callback: async (error, res, done) => {
        if (error) {
          console.log(error);
        } else {
          if (res.body.indexOf('403 Forbiddennginx') >= 0 || res.body.indexOf('检测到有异常请求从你的') >= 0) {
            console.log('IP被封掉了,咋整啊？\n');
            stopInterval()
            rl.prompt();
          } else {
            var data = JSON.parse(res.body);
            if (data.data.length !== 0) {
              data.data.forEach(function (item) {
                url.updateOne({
                  url: item.url
                }, {
                  url: item.url
                }, {
                  upsert: true
                }, function (error, docs) {
                  if (error) {
                    console.log(error);
                  } else {
                    if (docs.upserted !== undefined) {
                      console.log('成功' + nums);
                      nums++;
                    } else {}
                  }
                })
              })
            } else {
              // console.log('data没有数据');
              // var arr = guts.indexOf(res.options.uri);
              // var startStr = res.options.uri.indexOf('start=') + 6;
              // var endStr = res.options.uri.indexOf('&genres');
              // var startText = res.options.uri.substr(0, startStr);
              // var endText = res.options.uri.substr(endStr);
              // var endArr = startText + 900 + endText;
              // var endIndex = guts.indexOf(endArr);
              // guts.splice(arr, endIndex);
            }
          }
        }
        done();
      }
    })

    var a = 0;

    var shuff = shuffle(guts);

    function searchUrl() {

      repeat.findOne({
        url: shuff[a].url
      }, function (error, docs) {
        if (error) {
          console.log(error);
        } else {
          if (docs === null) {
            check.queue(shuff[a].url);
            repeat.updateOne({
              url: shuff[a].url
            }, {
              url: shuff[a].url
            }, {
              upsert: true
            }, function (error, docs) {
              if (error) {
                console.log(error);
              } else {}
            })
          } else {}
        }
      })
      a++;
    }
    var myInterval = setInterval(searchUrl, 50);

    function stopInterval() {
      clearInterval(myInterval);
    }
  })
})
db.on('error', function (error) {
  console.log('连接失败' + error);
});
db.on('disconnected', function () {
  console.log('连接断开');
})